package com.power.sifa.approve.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.approve.dao.Aprv_materials_customerDao;
import com.power.sifa.approve.model.Aprv_materials_customer;
import com.power.sifa.approve.model.enumeration.Tp_proctypeEnum;
import com.power.sifa.approve.model.pageInfo.Aprv_materials_customerPageInfo;
import com.power.sifa.approve.service.Aprv_materials_customerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;

/**
 *  自定义过程材料业务层实现
 */
@Service
public class Aprv_materials_customerServiceImpl extends BaseServiceImpl implements Aprv_materials_customerService {

    @Autowired
    private Aprv_materials_customerDao dao;


    @Override
    public Aprv_materials_customer getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Aprv_materials_customer aprv_materials_customer) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (aprv_materials_customer.getPrimaryKey() != null) {
        	 Aprv_materials_customer aprv_materials_customerfDb = dao.findById(aprv_materials_customer.getPrimaryKey()).orElse(null);
             if(aprv_materials_customerfDb != null) {
                BeanUtils.copyProperties(aprv_materials_customer, aprv_materials_customerfDb);
                dao.save(aprv_materials_customerfDb);
                return;
             }
        }
        // 获取当前最大的排序
        Integer sortNum = ObjectUtil.isNotEmpty(dao.queryMaxSotNumrByApplyid(aprv_materials_customer.getApplyid())) ?
                dao.queryMaxSotNumrByApplyid(aprv_materials_customer.getApplyid()) + 1 : 0;
        aprv_materials_customer.setSortnum(sortNum);
        dao.save(aprv_materials_customer);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Aprv_materials_customer> findAll(Aprv_materials_customerPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }
    
    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }
    
    @Override
	public List<Aprv_materials_customer> findAll() {
		return dao.findAll();
	}
	
	@Override
	public void saveAll(List<Aprv_materials_customer> list) {
		dao.saveAll(list);
	}

    /**
     * 通过applyid查询对应的自定义材料文件类型
     * @param applyid
     * @return
     */
    @Override
    public List<Aprv_materials_customer> listCustomerByApplyid(String applyid) {
        Specification<Aprv_materials_customer> specification = (root, query, criteriaBuilder) -> query.where(
                criteriaBuilder.and(criteriaBuilder.equal(root.get("applyid"), applyid))
        ).getRestriction();
        Sort sort = Sort.by(Sort.Direction.ASC, "sortnum");
        List<Aprv_materials_customer> customerMaterials = dao.findAll(specification, sort);
        return customerMaterials;
    }

    /**
     * 通过applyid和过程类型查询对应的自定义材料文件类型
     * @param applyid
     * @param proctypeEnum
     * @return
     */
    @Override
    public List<Aprv_materials_customer> listCustomerByProctype(String applyid, Tp_proctypeEnum proctypeEnum) {
        Specification<Aprv_materials_customer> specification = (root, query, criteriaBuilder) -> query.where(
                criteriaBuilder.and(criteriaBuilder.equal(root.get("applyid"), applyid), criteriaBuilder.equal(root.get("proctype"), proctypeEnum.getId()))
        ).getRestriction();
        Sort sort = Sort.by(Sort.Direction.DESC, "sortNum");
        List<Aprv_materials_customer> customerMaterials = dao.findAll(specification, sort);
        return customerMaterials;
    }


}
